load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
load("@npm_bazel_typescript//:index.bzl", "ts_library")
load("@npm_bazel_terser//:index.bzl", "terser_minified")
load("//rules/vendor:vendor.bzl", "vendor_bundle")

vendor_bundle(
  name = "vendor",
  deps = [
    "@npm//:node_modules/jquery/dist/jquery.js",
    "@npm//:node_modules/zone.js/dist/zone.js",
    "@npm//:node_modules/jquery-ui-dist/jquery-ui.js",
    "@npm//:node_modules/systemjs/dist/system.js"
  ]
)

terser_minified(
    name = "vendor_min",
    src = ":vendor",
)

ts_library(
    name = "ts_api",
    srcs = ["server.ts"],
    tsconfig = "//:tsconfig.json",
    deps = [
            "//src/apps/models", 
            "@npm//@types/node",
            "@npm//express",
            "@npm//compression",
            "@npm//request",
            ":data_files"
    ]
)

ts_library(
    name = "data_files",
    srcs = ["treeview-data.ts", "car-data.ts"],
    tsconfig = "//:tsconfig.json"
)

filegroup(
    name = "js_src",
    srcs = ["ts_api"],
    output_group = "es5_sources",
)


filegroup(
    name = "static",
    srcs = glob(["*.html", "*.css"]),
)

genrule(
    name = "bootstrap",
    srcs = ["//:node_modules/bootstrap/dist/css/bootstrap.min.css"],
    outs = ["bootstrap.min.css"],
    cmd = "cp $< $@",
)

nodejs_binary(
    name = "api", 
    data = [":ts_api", 
            ":static", 
            "@npm//express",
            ":bootstrap",
            ":vendor_min",
            "@npm//compression",
            "@npm//request",
            "//src/apps/demo-app:bundle-es5.min",
            "//src/apps/svelte-demo:bundle_svelte",
            "@npm//:node_modules/systemjs/dist/system.js"
    ],
    entry_point = "js_src",
)
